import { checkHttpStatus, parseJSON } from '../utils';
import { SEND_MESSAGE_REQUEST, SEND_MESSAGE_SUCCESS, SEND_MESSAGE_FAILURE, BAN_RESEND_MESS } from '../contants/index';
import { browserHistory } from 'react-router'

export function sendMessageRequest() {
	return {
		type: SEND_MESSAGE_REQUEST
	}
}

export function sendMessageSuccess() {
    return {
    	type: SEND_MESSAGE_SUCCESS
    }
}

export function sendMessageFailure() {
    return {
    	type: SEND_MESSAGE_FAILURE
    }
}

export function banResendMess(count) {
    return {
    	type: BAN_RESEND_MESS,
    	payload: {
    	    count: count
    	}
    }
}

export function sendMessage(userPhone) {
    return function(dispatch) {
    	dispatch(sendMessageRequest());
    	return fetch('http://fengke.net:8081/web/message', {
    		method: 'POST',
    		headers: {
    			'Accept': 'application/json',
    			'Content-Type': 'application/json'
    		},
    		body: JSON.stringify({
    			userPhone: userPhone
    		})
    	})
    	  .then(checkHttpStatus)
    	  .then(parseJSON)
    	  .then(
    	      response => {
                 if(response.status == "1") {
                 	dispatch(sendMessageSuccess());
                 } else {
                 	dispatch(sendMessageFailure())
                 }
    	      },
    	      error => {
                dispatch(sendMessageFailure())
    	      }
    	 )
    }
}
